package com.strange.mdl.module.mdl.service.incense;

import cn.hutool.core.collection.CollUtil;
import com.strange.mdl.module.mdl.controller.app.incense.vo.AppIncensePageReqVO;
import com.strange.mdl.module.mdl.controller.app.incense.vo.AppIncenseSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.strange.mdl.module.mdl.controller.admin.incense.vo.*;
import com.strange.mdl.module.mdl.dal.dataobject.incense.IncenseDO;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.pojo.PageParam;
import com.strange.mdl.framework.common.util.object.BeanUtils;

import com.strange.mdl.module.mdl.dal.mysql.incense.IncenseMapper;

import static com.strange.mdl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.strange.mdl.framework.common.util.collection.CollectionUtils.convertList;
import static com.strange.mdl.framework.common.util.collection.CollectionUtils.diffList;
import static com.strange.mdl.module.mdl.enums.ErrorCodeConstants.*;

/**
 * 供香记录 Service 实现类
 *
 * @author 密谛灵总控
 */
@Service
@Validated
public class IncenseServiceImpl implements IncenseService {

    @Resource
    private IncenseMapper incenseMapper;

    @Override
    public Long createIncense(IncenseSaveReqVO createReqVO) {
        // 插入
        IncenseDO incense = BeanUtils.toBean(createReqVO, IncenseDO.class);
        incenseMapper.insert(incense);
        // 返回
        return incense.getId();
    }

    @Override
    public Long appCreateIncense(AppIncenseSaveReqVO createReqVO) {
        // 插入
        IncenseDO incense = BeanUtils.toBean(createReqVO, IncenseDO.class);
        incenseMapper.insert(incense);
        // 返回
        return incense.getId();
    }

    @Override
    public void updateIncense(IncenseSaveReqVO updateReqVO) {
        // 校验存在
        validateIncenseExists(updateReqVO.getId());
        // 更新
        IncenseDO updateObj = BeanUtils.toBean(updateReqVO, IncenseDO.class);
        incenseMapper.updateById(updateObj);
    }

    @Override
    public void deleteIncense(Long id) {
        // 校验存在
        validateIncenseExists(id);
        // 删除
        incenseMapper.deleteById(id);
    }

    @Override
        public void deleteIncenseListByIds(List<Long> ids) {
        // 校验存在
        validateIncenseExists(ids);
        // 删除
        incenseMapper.deleteByIds(ids);
        }

    private void validateIncenseExists(List<Long> ids) {
        List<IncenseDO> list = incenseMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(INCENSE_NOT_EXISTS);
        }
    }

    private void validateIncenseExists(Long id) {
        if (incenseMapper.selectById(id) == null) {
            throw exception(INCENSE_NOT_EXISTS);
        }
    }

    @Override
    public IncenseDO getIncense(Long id) {
        return incenseMapper.selectById(id);
    }

    @Override
    public PageResult<IncenseDO> getIncensePage(IncensePageReqVO pageReqVO) {
        return incenseMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<IncenseDO> getAppIncensePage(AppIncensePageReqVO pageReqVO) {
        return incenseMapper.selectAppPage(pageReqVO);
    }

}